package eu.tanov.epf.pv.types.project.ui.pages;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.epf.library.edit.command.IActionManager;
import org.eclipse.epf.library.services.LibraryModificationHelper;
import org.eclipse.epf.library.util.LibraryManager;
import org.eclipse.epf.uma.CustomCategory;
import org.eclipse.epf.uma.Section;
import org.eclipse.epf.uma.UmaFactory;
import org.eclipse.epf.uma.UmaPackage;
import org.eclipse.jface.wizard.Wizard;

import eu.tanov.epf.pv.types.projectiteration.common.util.ProjectIterationHelper;
import eu.tanov.epf.pv.types.projectpractice.common.util.ProjectPracticeHelper;

public class ProjectIterationWizard extends Wizard {

	private ProjectIterationWizardData data;
	private ProjectProjectIterationsPage page;
	private Set<CustomCategory> practices = new HashSet<CustomCategory>();
	private Set<CustomCategory> projPractices = new HashSet<CustomCategory>();
	protected ArrayList usedCategories = new ArrayList();
	protected IActionManager actionMgr;

	public ProjectIterationWizard(ProjectProjectIterationsPage page) {
		super();
		initData();
		this.page = page;
		actionMgr = new LibraryModificationHelper().getActionManager();
	}

	private void initData() {
		data = new ProjectIterationWizardData();
	}

	@Override
	public boolean performFinish() {
		CustomCategory newIteration = page.createNewIteration();

		practices.addAll(projPractices);
		addItemsToModel1(newIteration, new ArrayList<CustomCategory>(practices));

		return true;
	}

	@Override
	public void addPages() {
		setWindowTitle("Create new Iteration");
		addPage(new MethodAndPracticePage("Methods and Practices", data));
		addPage(new FeaturedPracticesFromIterationsPage("Project Practices", data, page.getContainer(), practices));
		addPage(new FeaturedPracticesFromOtherProjectsPage("Other Used Practices", data, page.getContainer(), projPractices));
	}

	protected void addItemsToModel1(CustomCategory newIteration, ArrayList<CustomCategory> objectsToAdd) {

		for (CustomCategory cat : objectsToAdd) {
			addToCategory(actionMgr, newIteration, cat);
		}

		addPracticeSections(newIteration, objectsToAdd);
	}

	protected CustomCategory addToCategory(IActionManager actionMgr, CustomCategory categoryToModify, CustomCategory objectToAdd) {
		final EStructuralFeature feature = UmaPackage.eINSTANCE.getCustomCategory_CategorizedElements();
		return (CustomCategory) LibraryManager.getInstance().addToCategory(actionMgr, categoryToModify, objectToAdd, feature,
				ProjectIterationHelper.PROJECT_ITERATIONS_PATH, true, usedCategories);
	}

	protected void addPracticeSections(CustomCategory newIteration, List objectsToAdd) {
		for (Object o : objectsToAdd) {
			if (ProjectPracticeHelper.isProjectPractice(o)) {
				CustomCategory cat = (CustomCategory) o;
				// XXX - practice persistence
				List<Section> sections = newIteration.getPresentation().getSections();
				Section newMethod = UmaFactory.eINSTANCE.createSection();
				newMethod.setSectionName(cat.getName());
				setSection(sections, newMethod);
			}
		}

		// HAX
		actionMgr.doAction(IActionManager.SET, newIteration.getPresentation(), UmaPackage.eINSTANCE.getMethodUnit_ChangeDate(), new Date(), -1);
	}

	private void setSection(List<Section> sections, Section newSection) {
		for (int i = 0; i < sections.size(); ++i) {
			if (sections.get(i).getSectionName().equals(newSection.getSectionName())) {
				return;
			}
		}

		sections.add(newSection);
	}

}
